<template>
  <div>
    <!--搜索条件-->
    <el-form inline :model="searchParam" ref="searchParam" label-width="80px" class="search-form">
      <el-form-item label="表名" prop="tableName">
        <el-input v-model="searchParam.tableName" placeholder="表名" clearable></el-input>
      </el-form-item>
      <el-form-item label="表备注" prop="tableComment">
        <el-input v-model="searchParam.tableComment" placeholder="表备注" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchForm">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetSearchForm">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button type="primary"  @click="generators()"><i class="fa fa-rocket mr-8"></i>生成代码</el-button>
    </el-row>
    <!--表格-->
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      element-loading-text="数据加载中"
      @selection-change="handleSelectionChange"
      class="mt-20">

      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="tableName"
        label="表名"
        sortable
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="engine"
        label="Engine"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="tableComment"
        show-overflow-tooltip
        label="表备注">
      </el-table-column>
      <el-table-column
        prop="createTime"
        show-overflow-tooltip
        label="创建时间">
        <template slot-scope="scope">
          <es-time  :value="scope.row.createTime" type="format" format="yyyy-MM-dd hh:mm:ss"></es-time>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        show-overflow-tooltip
        width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="generators(scope.row.tableName)">生成代码
          </el-button>

          <el-button
            size="mini"
            type="primary"
            @click="genForm(scope.row.tableName)">表单生成
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      style="margin: 20px;float: right;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="searchParam.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  </div>
</template>

<script>
  import {getList,genCode} from '../../api/sys/generatorApi'
  export default {
    name: 'dictList',
    data () {
      return {
        searchParam: {//表格数据请求参数
          page: 1,
          limit: 10
        },
        loading: false,
        multipleSelection: [], //复选框选中的值
        tableData: [],
        total: 0,
        ids:[],
      }
    },
    mounted () {
      //获取数据
      this.getTableData()
    },
    methods: {
      //生成表单
      genForm(tableName){
         this.$router.push("/generator/genForm?tableName="+tableName);
      },
      //生成代码
      generators: function (tableName) {
        let tableNames = "";
        if(tableName){
          tableNames=tableName;
        }else{
          let chcekeds = this.multipleSelection;
          if (chcekeds.length === 0) {
            this.$message.error("至少一条数据");
            return false;
          }
          for (let i = 0, len = chcekeds.length; i < len; i++) {
            tableNames += chcekeds[i].tableName;
            if (i < len - 1) {
              tableNames += ",";
            }
          }
        }
        //生成代码
        genCode(tableNames);
      },
      //获取数据
      getTableData () {
        let _this = this
        _this.loading = true
        getList(_this.searchParam).then((res) => {
          if (res.code === 200) {
            _this.tableData = res.data
            console.log(_this.tableData)
            _this.total = res.total
          } else {
            this.$message.error(res.msg)
          }
          _this.loading = false
        })
      },
      //获取选中行
      getSelectIds: function (row) {
        let chcekeds = [];
        let ids = [];
        if (!row || Object.keys(row).length === 0) {
          chcekeds = this.multipleSelection;
          if (chcekeds.length === 0) {
            this.$message.error("至少选择一条数据");
          } else {
            for (let key in chcekeds) {
              ids.push(chcekeds[key].id);
            }
          }
        } else {
          ids.push(row.id);
        }
        return ids;

      },
      //查询表格
      searchForm () {
        this.searchParam.page = 1
        //获取数据
        this.getTableData()
      },
      //重置查询表格
      resetSearchForm () {
        this.$refs['searchParam'].resetFields()
        //获取数据
        this.getTableData()
      },
      handleSizeChange (limit) {
        this.searchParam.limit = limit
        //获取数据
        this.getTableData()
      },
      handleCurrentChange (page) {
        this.searchParam.page = page
        //获取数据
        this.getTableData()
      },
      //复选框选择
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
    }
  }
</script>

<style scoped>

</style>
